<?php

namespace YmPhp\Admin\Http\Controllers\Business;

use YmPhp\Admin\Http\Controllers\AdminController;
use YmPhp\Admin\Http\Repositories\Business\BusinessList;
use YmPhp\Admin\Form;
use YmPhp\Admin\Grid;
use YmPhp\Admin\Models\Member\MemberList;
use YmPhp\Admin\Models\SiteCity\SiteCity;
use YmPhp\Admin\Show;

class BusinessListController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new BusinessList(), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('uid')->display(function ($value) {
                $member = (new MemberList())->getDetails($this->uid);
                $siteCity = (new SiteCity())->getCity($this->stid);
                $html = '<div class="">';
                if (!empty($member) && $member->username != ''){
                    $html .= $member->username.'<br>';
                    $html .= 'UID:'.$value.'<br>';
                }else {
                    $html .= '尚未被认领<br>';
                    $html .= 'UID:未知<br>';
                }
                $html .= ''.$siteCity->name.'</div>';
                return $html;
            });
            $grid->column('city','所属行业')->display(function ($value) {
                $hangye = explode(' ', $this->hangye);
                $html = '';
                foreach ($hangye as  $value) {
                    $html .= $value."<br>";
                }
                return $html;
            });
            $grid->column('name')->display(function ($value) {
                $html = '<div style="display: flex;">';
                    $html .= '<div>';
                        $html .= '<img data-action="preview-img" src="'.$this->logo.'" style="height: 70px;width:70px;border-radius: 5px;cursor:pointer" class="img img-thumbnail"/>';

                    $html .= '</div>';

                $html .= '<div style="margin-left: 10px;"> <span style="font-weight: bold">'.$value.'</span>';

                $html .= '<div style="margin-top: 5px;">';
                $html .= '<div>营业时间：'.$this->opentime.'</div>';
                $html .= '</div>';
                $html .= '<div style="margin-top: 5px;">'.$this->tel.'</div>';
                $html .= '</div>';
                return $html;
            });
            $grid->column('addr')->display(function ($value) {

                $color = ['#3275fa','#20c997','#edc30e','#22292f'];
                $html = '<div style="display: flex;flex-direction: column;">';
                if ($this->tags) {
                    $html = '<div style="display: flex;">';
                    $slicedArray = array_slice(explode(',', $this->tags), 0, 3);
                    foreach ($slicedArray as $key => $tag) {
                        $html .= '<span class="label label-success" style="background:'.$color[$key].';margin-right: 5px">' . $tag . '</span>';
                    }
                    $html .= '</div>';
                }
                $html .= '<div style="margin-top: 5px;">'.$this->addr.'</div>';
                $html .= '</div>';
                return $html;
            })->width(200);
//            $grid->column('jieshao')->limit(50)->width(300);
            $grid->column('tui_jian')->switch();
            $grid->column('display')->switch();
            $grid->column('end_at')->width(100);
            $grid->column('top_end_at')->width(100)->display(function ($value) {
                if ( !empty($value)) {
                    return $value;
                }else{
                    return '未置顶';
                }
            });
            $grid->column('created_at')->width(100);
            $grid->column('updated_at')->width(100);
            $grid->disableViewButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('shid');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new BusinessList(), function (Show $show) {
            $show->field('shid');
            $show->field('uid');
            $show->field('display');
            $show->field('name');
            $show->field('hangye');
            $show->field('hangye_id1');
            $show->field('hangye_id2');
            $show->field('tel');
            $show->field('opentime');
            $show->field('jieshao');
            $show->field('logo');
            $show->field('qr');
            $show->field('album');
            $show->field('viptype');
            $show->field('lat');
            $show->field('lng');
            $show->field('province');
            $show->field('city');
            $show->field('district');
            $show->field('street');
            $show->field('street_number');
            $show->field('addr');
            $show->field('tags');
            $show->field('views');
            $show->field('shares');
            $show->field('comments');
            $show->field('pubs');
            $show->field('follow');
            $show->field('tag');
            $show->field('shipin');
            $show->field('quanjing');
            $show->field('xuanchuan');
            $show->field('color');
            $show->field('color_title');
            $show->field('shangquan');
            $show->field('append_img');
            $show->field('append_text');
            $show->field('links');
            $show->field('hong_num');
            $show->field('hong_money');
            $show->field('hong_sendnum');
            $show->field('shzhangqi');
            $show->field('shinsxf');
            $show->field('shdiscount');
            $show->field('stid');
            $show->field('hxpwd');
            $show->field('mp3');
            $show->field('top_start_at');
            $show->field('top_end_at');
            $show->field('stids');
            $show->field('stida');
            $show->field('renewal_notice_at');
            $show->field('cover');
            $show->field('maidanrate');
            $show->field('maidanmin');
            $show->field('video');
            $show->field('video_cover');
            $show->field('hangye_ids');
            $show->field('guimo');
            $show->field('dongjie');
            $show->field('appid');
            $show->field('appkey');
            $show->field('device_id');
            $show->field('end_at');
            $show->field('hbtiaojian');
            $show->field('bohao');
            $show->field('tui_jian');
            $show->field('baoyou');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BusinessList(), function (Form $form) {
            $form->display('shid');
            $form->display('uid');
            $form->text('name');
            $form->select('hangye') ->options([])->loads(['hangye_id1', 'hangye_id2'], ['/api/field1', '/api/field2']);;
            $form->text('hangye_id1');
            $form->text('hangye_id2');
            $form->text('tel');
            $form->text('opentime');
            $form->text('jieshao');
            $form->image('logo')->autoUpload()->uniqueName()->saveFullUrl();
            $form->text('qr');
            $form->text('album');
            $form->text('viptype');
            $form->text('lat');
            $form->text('lng');
            $form->text('province');
            $form->text('city');
            $form->text('district');
            $form->text('street');
            $form->text('street_number');
            $form->text('addr');
            $form->text('tags');
            $form->text('views');
            $form->text('shares');
            $form->text('comments');
            $form->text('pubs');
            $form->text('follow');
            $form->text('tag');
            $form->text('shipin');
            $form->text('quanjing');
            $form->text('xuanchuan');
            $form->text('color');
            $form->text('color_title');
            $form->text('shangquan');
            $form->text('append_img');
            $form->text('append_text');
            $form->text('links');
            $form->text('hong_num');
            $form->text('hong_money');
            $form->text('hong_sendnum');
            $form->text('shzhangqi');
            $form->text('shinsxf');
            $form->text('shdiscount');
            $form->text('stid');
            $form->text('hxpwd');
            $form->text('mp3');
            $form->text('top_start_at');
            $form->text('top_end_at');
            $form->text('stids');
            $form->text('stida');
            $form->text('renewal_notice_at');
            $form->text('cover');
            $form->text('maidanrate');
            $form->text('maidanmin');
            $form->text('video');
            $form->text('video_cover');
            $form->text('hangye_ids');
            $form->text('guimo');
            $form->switch('dongjie');
            $form->text('appid');
            $form->text('appkey');
            $form->text('device_id');
            $form->text('end_at');
//            $form->text('hbtiaojian');
            $form->number('bohao');
            $form->switch('tui_jian');
            $form->currency('baoyou')->symbol('￥')->width(2);
            $form->switch('display');
        });
    }
}
